package net.huanger.gametools.mapeditor.ui.impl;


import java.awt.Point;
import java.awt.event.MouseEvent;

import javax.swing.JComponent;
import javax.swing.SwingUtilities;

import net.huanger.gametools.mapeditor.model.MapModel;
import net.huanger.gametools.mapeditor.ui.MUtils;


public class BlockOperate extends AbstractDataOperate
{
  final public int BLOCK=1;
  final public int PASS=0;
  public BlockOperate(int row, int column)
  {
    super(row, column);
  }

  @Override
  public void init()
  {

  }

  @Override
  public void titeClick(MouseEvent e, int row, int column)
  {
  }

  @Override
  public void mapClick(MouseEvent e)
  {
    int selectCell=MUtils.pointToCell(e.getPoint(), mapRow, mapColumn);
    if(selectCell!=MUtils.OUT_LIMIT){
      Point p=MUtils.cellToPoint(selectCell, mapRow, mapColumn);
       if(SwingUtilities.isRightMouseButton(e)){  //右键设置pass
         //如果本来就是pass则返回
         if(getCellData(selectCell)==PASS)return;
        getImage().setRGB(p.x, p.y, MUtils.MAP_UNIT, MUtils.MAP_UNIT, MUtils.TRANSPARENT_DATA, 0, MUtils.MAP_UNIT);
        //设置此cell的数据为pass
        setCellData(selectCell, PASS);
       }else if(SwingUtilities.isLeftMouseButton(e)){//左键设置block
         //如果本来就是block则返回
         if(getCellData(selectCell)==BLOCK)return;
         getImage().setRGB(p.x, p.y, MUtils.MAP_UNIT, MUtils.MAP_UNIT, MUtils.BLOCK_DATA, 0, MUtils.MAP_UNIT);
         //设置此cell的数据为block
         setCellData(selectCell, BLOCK);
       }
       ((JComponent)e.getSource()).repaint(p.x, p.y, MUtils.MAP_UNIT, MUtils.MAP_UNIT);
    }
  }

  @Override
  public void restoreData(MapModel mm)
  {
    int[]data=mm.getBlock();
    for(int i=0;i<data.length;i++){
      setCellData(i, data[i]);
      if(data[i]==BLOCK){
        Point p=MUtils.cellToPoint(i, mapRow, mapColumn);
        getImage().setRGB(p.x, p.y, MUtils.MAP_UNIT, MUtils.MAP_UNIT, MUtils.BLOCK_DATA, 0, MUtils.MAP_UNIT);
      }
    }
  }

}
